<template>
  <div id="app" class='app' @click='freshLoginStatusTimer'>
    <router-view></router-view>
  </div>
</template>
<script>
import {
  mapState,
  mapGetters,
  mapMutations,
  mapActions
} from 'vuex'

import VConsole from 'vconsole';
import {
  Toast,
} from 'vant';
import {
  getQueryString
} from '@/utils/index'
import windowsSDK from '@/common/windows-sdk';

import {getMachineId} from '@/api/index'


import {
  connectSlipPrinter,
} from '@/api/xuhui-android'
import {getDeviceInfo, getDeviceRenewalRecord} from "@/api/common";

export default {
  data() {
    return {
      startPageLoading: false,
    };
  },
  computed: {
    ...mapState(['system','deviceInfo'])

  },

  mounted() {
    this.startPageLoading = true;
    this.init();
  },
  methods: {
    ...mapActions(['freshLoginStatusTimer']),
    ...mapMutations(['setSystem', 'setDeviceInfo']),
    init() {
      let sdkModule = getQueryString('sdkModule');
      if (sdkModule) {
        this.setSystem({
          sdkModule: sdkModule,
        })
      }
      XhAggregation.init({
        success: async () => {
          await this.getSerialNumber();
        },
        fail: () => {

        }
      })
    },
    //获取终端 SN 号
    getSerialNumber() {
      XhAggregation.getSerialNumber({
        success: async (res) => {
          let sn;
          if (this.system.sdkModule == 'XH') {
            sn = res.serialNumber
          } else {
            sn = res.data.sn
          }
          this.setDeviceInfo({
            sn, //设备sn号
          })
          // console.log('res:getSerialNumber', res,this.system.sdkModule);
          if (this.system.sdkModule == 'XH') {
            //设备
            const res2=await getDeviceInfo({
              sn
            })
            // console.log(res2,'getDeviceInfo')
            if(res2.success){
              const addressName = res2.data && res2.data.CityArrName.split('/')[1]
              this.setDeviceInfo({
                addressName:addressName
              })
              console.log('res:getDeviceInfo', this.deviceInfo)
            }
            //时间
            const res3=await getDeviceRenewalRecord({
              sn
            })
            if(res3.success){
              this.setDeviceInfo({
                ExpirationTime: res3.data && res3.data.ExpirationTime
              })
            }
            if(res2.success&&res3.success){

              this.$router.push({
                path: "index",
              })
            }
          } else {
            await XhAggregation.getConfig({
              success: async (res) => {
                console.log('res:getConfig', res);
                const addressName = res.data && res.data.CityArrName.split('/')[1]
                this.setDeviceInfo({
                  addressName,
                  ExpirationTime: res.data && res.data.ExpirationTime
                })

                this.$router.push({
                  path: "index",
                })
              }
            })
          }

        },
        fail: (err) => {
          console.log(err)
          this.$router.push({
            path: "index",
          })
        }
      })
    },
  }
}
</script>
<style lang="less">
html {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
}
</style>
